package com.code2roc.jarmanage.servlet.firewall;

import com.code2roc.jarmanage.db.FireWallRuleDB;
import com.code2roc.jarmanage.firewall.FireWallService;
import com.code2roc.jarmanage.firewall.FireWallServiceFactory;
import com.code2roc.jarmanage.model.FireWallRuleDO;
import com.code2roc.jarmanage.model.Result;
import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.ConvertUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class FireWallDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        HashMap paramMap = CommonUtil.getParamMap(req);
        String ruleID = ConvertUtil.convert2String(paramMap.get("ruleID"));
        FireWallRuleDO ruleDO = FireWallRuleDB.selectOne(ruleID);
        FireWallService service = new FireWallServiceFactory().CreateFireWallService();
        if(service.deleteRule(ruleDO.getRuleName(),ruleDO.getRuleProtocol(),ruleDO.getRulePort(),ruleDO.getRuleAction())){
            FireWallRuleDB.delete(ruleID);
        }else{
            result = Result.errorResult();
        }
        resp.getWriter().println(result.toString());
    }
}
